import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ost_flutter_module/common/adapt.dart';
import 'package:ost_flutter_module/common/color.dart';

import 'textfield_formatter.dart';

class TextFieldWidget extends StatefulWidget {
  final String titleText;
  final String hintTtext;
  final bool showLine;
  final bool isPassword;
  final bool isMobile;
  final int maxLength;
  final TextInputType inputType;
  final ValueChanged<String> onChanged;

  const TextFieldWidget(
      {Key key,
      @required this.titleText,
      this.hintTtext,
      this.isMobile = false,
      this.maxLength = 30,
      this.inputType = TextInputType.text,
      this.showLine = true,
      this.isPassword = true,
      this.onChanged})
      : super(key: key);

  @override
  TextFieldWidgetState createState() => TextFieldWidgetState();
}

class TextFieldWidgetState extends State<TextFieldWidget> {
  TextEditingController _controller;

  @override
  void initState() {
    super.initState();
    _controller = TextEditingController();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  void setContentText(String text) => _controller?.text = text;

  String get contentText => _controller?.text;

  @override
  Widget build(BuildContext context) {
    List<TextInputFormatter> formatters = [];
    if (widget.isMobile) {
      formatters.add(PhoneNumberFormatter());
      formatters.add(LengthLimitingTextInputFormatter(13));
    } else {
      formatters.add(LengthLimitingTextInputFormatter(widget.maxLength));
    }

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: EdgeInsets.symmetric(horizontal: Adapt.px(16)),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            children: [
              Expanded(
                flex: 2,
                child: Text(widget.titleText,
                    style:
                        TextStyle(fontSize: Adapt.px(16), color: color333333)),
              ),
              Expanded(
                flex: 4,
                child: TextField(
                  controller: _controller,
                  obscureText: widget.isPassword,
                  keyboardType: widget.inputType,
                  inputFormatters: formatters,
                  style: TextStyle(fontSize: Adapt.px(16), color: color333333),
                  onChanged: widget.onChanged,
                  decoration: InputDecoration(
                    border: InputBorder.none,
                    contentPadding:
                        EdgeInsets.symmetric(vertical: Adapt.px(20)),
                    hintText: widget.hintTtext,
                    hintStyle:
                        TextStyle(fontSize: Adapt.px(16), color: colorDDDDDD),
                  ),
                ),
              ),
            ],
          ),
        ),
        widget.showLine
            ? Padding(
                padding: EdgeInsets.only(left: Adapt.px(16)),
                child: Divider(
                  height: Adapt.px(2),
                  color: colorE5E5E5,
                ),
              )
            : Container()
      ],
    );
  }
}
